<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <div>
        <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50">
          <ArticleItem v-for="item in articleList"
            :key="item.art_id" :obj="item">
           </ArticleItem>
        </van-list>
      </div>
    </div>
</template>

<script>
import { timeAgo } from '@/utils/date'
import { searchResultListAPI } from '@/api'
import ArticleItem from '../Home/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  async created () {
    this.getSearchResultFn()
  },
  components: {
    ArticleItem
  },
  data () {
    return {
      articleList: [],
      loading: false, // 加载状态
      finished: false, // 全部加载完成状态
      page: 1
    }
  },
  methods: {
    onLoad () {
      if (this.articleList.length > 0) {
        this.page++
        this.getSearchResultFn()
      }
    },
    async getSearchResultFn () {
      const res = await searchResultListAPI({
        q: this.$route.params.keywords,
        page: this.page
      })
      console.log(this.page)
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      // console.log(res.data.data.results[0].title)
      res.data.data.results.forEach(item => {
        item.pubdate = timeAgo(item.pubdate)
      })
      this.articleList = [...this.articleList, ...res.data.data.results]
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
